package com.uls.web.controller.hdh;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.uls.hdh.domain.*;
import com.uls.hdh.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 投票事项Controller
 * 
 * @author hdh
 * @date 2024-11-08
 */
@RestController
@RequestMapping("/hdh/polls")
public class HdhPollsController extends BaseController
{
    @Autowired
    private IHdhPollsService hdhPollsService;
    @Autowired
    private IHdhOptionsService hdhOptionsService;
    @Autowired
    private IHdhVotesService hdhVotesService;
    @Autowired
    private IHdhUsersService hdhUsersService;
    @Autowired
    private IHdhUserWeightsService hdhUserWeightsService;
    @Autowired
    private IHdhPollResultsService hdhPollResultsService;
    /**
     * 查询投票事项列表
     */
    @PreAuthorize("@ss.hasPermi('hdh:polls:list')")
    @GetMapping("/list")
    public TableDataInfo list(HdhPolls hdhPolls)
    {
        startPage();
        List<HdhPolls> list = hdhPollsService.selectHdhPollsList(hdhPolls);
        for (HdhPolls hdhPolls1:list){
            Long optionId=hdhPolls1.getOptionId();
            HdhOptions hdhOptions=hdhOptionsService.selectHdhOptionsByOptionId(optionId);
            if(hdhOptions!=null){
                hdhPolls1.setOptionText(hdhOptions.getOptionText());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出投票事项列表
     */
    @PreAuthorize("@ss.hasPermi('hdh:polls:export')")
    @Log(title = "投票事项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HdhPolls hdhPolls)
    {
        List<HdhPolls> list = hdhPollsService.selectHdhPollsList(hdhPolls);
        ExcelUtil<HdhPolls> util = new ExcelUtil<HdhPolls>(HdhPolls.class);
        util.exportExcel(response, list, "投票事项数据");
    }

    /**
     * 获取投票事项详细信息
     */
    @PreAuthorize("@ss.hasPermi('hdh:polls:query')")
    @GetMapping(value = "/{pollId}")
    public AjaxResult getInfo(@PathVariable("pollId") Long pollId)
    {
        return success(hdhPollsService.selectHdhPollsByPollId(pollId));
    }

    /**
     * 新增投票事项
     */
    @PreAuthorize("@ss.hasPermi('hdh:polls:add')")
    @Log(title = "投票事项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HdhPolls hdhPolls)
    {
        hdhPolls.setCreatedBy(this.getUsername());
        hdhPolls.setCreator(this.getUsername());
        hdhPolls.setCreatedTime(new Date());
        return toAjax(hdhPollsService.insertHdhPolls(hdhPolls));
    }

    /**
     * 修改投票事项
     */
    @PreAuthorize("@ss.hasPermi('hdh:polls:edit')")
    @Log(title = "投票事项", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody HdhPolls hdhPolls)
    {
        hdhPolls.setUpdatedBy(this.getUsername());
        hdhPolls.setUpdatedTime(new Date());
        return toAjax(hdhPollsService.updateHdhPolls(hdhPolls));
    }

    /**
     * 删除投票事项
     */
    @PreAuthorize("@ss.hasPermi('hdh:polls:remove')")
    @Log(title = "投票事项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pollIds}")
    public AjaxResult remove(@PathVariable Long[] pollIds)
    {
        return toAjax(hdhPollsService.deleteHdhPollsByPollIds(pollIds));
    }


    /**
     * 提交申请
     */
    @PreAuthorize("@ss.hasPermi('hdh:polls:pollsSubmit')")
    @Log(title = "投票事项", businessType = BusinessType.EXPORT)
    @GetMapping("/submitPolls/{pollId}")
    public AjaxResult submitPolls(@PathVariable Long pollId)
    {
        return  toAjax(hdhPollsService.submit(pollId,getUserId()));
    }

    ////审批相关

    /**
     * 查询未审批的信息列表
     */

    @PreAuthorize("@ss.hasPermi('hdh:polls:listNo')")
    @GetMapping("/listNo")
    public TableDataInfo listNo(HdhPolls hdhPolls)
    {
        startPage();
        // List<HdhPolls> list = hdhPollsService.noFinishTask(getUserId(),"myVoteProcess");//关联工作流没有带条件查询
        List<HdhPolls> list = hdhPollsService.noFinishTask(getUserId(),"myVoteProcess",hdhPolls);//关联工作流带条件查询
        return getDataTable(list);
    }
    /**
     * 获取信息详细信息
     */
    @GetMapping(value = "/getHdhPollsComment/{pollId}")
    public AjaxResult getInfoComment(@PathVariable("pollId") Long pollId)
    {
        HdhPolls hdhPolls=hdhPollsService.selectHdhPollsByPollId(pollId);

        hdhPolls.setTaskCommentVoList(hdhPollsService.getTaskCommentList(hdhPolls.getFlowInstantId()));  //找出前面审批人的审批信息

        return success(hdhPolls);
    }
    /**
     * 查询未审批的信息列表
     */
    @PreAuthorize("@ss.hasPermi('hdh:polls:editnoapprove')")
    @Log(title = "查询未审批的信息列表", businessType = BusinessType.EXPORT)
    @PostMapping("/editnoapprove")
    public AjaxResult editSaveNoAuthData(@RequestBody HdhPolls hdhPolls)
    {
        System.out.println(hdhPolls.toString());
        System.out.println(hdhPolls.getAgree());
        hdhPollsService.completeTask(hdhPolls.getFlowInstantId(),hdhPolls.getAgree(),getUserId(),hdhPolls.getComment(),hdhPolls.getPollId());
        return  toAjax(200);
    }

    @PreAuthorize("@ss.hasPermi('hdh:polls:listFinished')")
    @GetMapping("/listFinished")
    public TableDataInfo listFinished(HdhPolls hdhPolls)
    {
        startPage();
        //List<HdhPolls> list = hdhPollsService.finishTask(getUserId(),"myVoteProcess");//关联工作流没有带条件查询
        List<HdhPolls> list = hdhPollsService.finishTask(getUserId(),"myVoteProcess",hdhPolls);//关联工作流带条件查询
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('hdh:polls:listpollsvoted')")
    @GetMapping("/listpollsvoted")
    public TableDataInfo listpollsvoted(HdhPolls hdhPolls)
    {
        startPage();
        HdhPolls hdhPolls1=new HdhPolls();
        hdhPolls1.setStatus(3l);
        List<HdhPolls> list = hdhPollsService.selectHdhPollsList(hdhPolls1);
        return getDataTable(list);
    }

    @GetMapping("/getAllpolls")
    public AjaxResult getAllpolls()
    {
        List<HdhPolls> list = hdhPollsService.selectHdhPollsList(null);
        return success(list);
    }

    /**
     * 投票事项
     */
    @PreAuthorize("@ss.hasPermi('hdh:polls:edit')")
    @Log(title = "投票事项", businessType = BusinessType.UPDATE)
    @PutMapping("/vote")
    public AjaxResult vote(@RequestBody HdhPolls hdhPolls)
    {
        hdhPollsService.updateHdhPolls(hdhPolls);
        HdhVotes hdhVotes=new HdhVotes();
        hdhVotes.setPollId(hdhPolls.getPollId());
        Long id=hdhUsersService.getUserIdByUsernumber(Long.valueOf(this.getUsername()));
        hdhVotes.setUsersId(id);
        hdhVotes.setOptionId(hdhPolls.getOptionId());
        hdhVotes.setVoteTime(new Date());
        HdhUserWeights hdhUserWeights=hdhUserWeightsService.selectHdhUserWeightsByUsersId(id);
        hdhVotes.setWeightFactor(hdhUserWeights.getWeightFactor());
        hdhVotes.setIsAnonymous(hdhPolls.getIsAnonymous());
        if(hdhPolls.getIsAnonymous()==1){
            hdhVotes.setUsersId(id);
        }else hdhVotes.setUsersId(0l);
        hdhVotesService.insertHdhVotes(hdhVotes);

        System.out.println(11);
        HdhPollResults hdhPollResults=new HdhPollResults();
        hdhPollResults.setPollId(hdhPolls.getPollId());
        hdhPollResults.setOptionId(hdhPolls.getOptionId());
        List<HdhPollResults> results=hdhPollResultsService.selectHdhPollResultsList(hdhPollResults);
        HdhPollResults firstResult = results.get(0);
        long total=firstResult.getTotalVotes();
        long factor=firstResult.getWeightedVotes();
        firstResult.setTotalVotes(total+1);
        firstResult.setWeightedVotes(factor+hdhUserWeights.getWeightFactor());
        hdhPollResultsService.updateHdhPollResults(firstResult);
        return toAjax(true);
    }



}
